﻿using Microsoft.AspNetCore.Http;
using System.Linq;
using System.Threading.Tasks;
using WebApp.Models;

namespace WebApp
{
    public class TestMiddleware
    {
        private RequestDelegate nextDelegate;

        public TestMiddleware(RequestDelegate nextDelegate)
        {
            this.nextDelegate = nextDelegate;
        }

        public async Task Invoke(HttpContext context, DataContext dataContext)
        {
            if (context.Request.Path == "/test") {
                await context.Response.WriteAsync($"There are {dataContext.Products.Count()} prodcts.\n");
                await context.Response.WriteAsync($"There are {dataContext.Categories.Count()} categories.\n");
                await context.Response.WriteAsync($"There are {dataContext.Suppliers.Count()} suppliers.\n");
            } else {
                await nextDelegate(context);
            }
        }
    }
}
